﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.DrawingStandard
{
    public static class TablePloter
    {
        public static void CreatTable(Point2d midtop,ref Extents2d ext, double columnW, double rowH, ref System.Data.DataTable theTable)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            //DBObjectCollection ret = new DBObjectCollection();
            Table tb = new Table();

            double w = columnW * theTable.Columns.Count;
            double h = rowH * (theTable.Rows.Count + 1);

            tb.SetSize(theTable.Rows.Count + 1, theTable.Columns.Count);       // 设置几行几列
            tb.Position = midtop.C3D(-w * 0.5);


            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
           OpenMode.ForWrite) as BlockTableRecord;

                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                for (int i = 0; i < theTable.Columns.Count; ++i)
                {
                    if (theTable.Columns[i].Caption != "")
                    {
                        tb.SetTextString(0, i, theTable.Columns[i].Caption);
                        //tb.Cells[0, i].TextString = theTable.Columns[i].Caption;
                    }
                    else
                    {
                        tb.SetTextString(0, i, theTable.Columns[i].ColumnName);
                        //tb.Cells[0, i].TextString = theTable.Columns[i].ColumnName;
                    }
                    tb.SetTextHeight(0, i, 2.5);
                    tb.SetAlignment(0, i, CellAlignment.MiddleCenter);
                    tb.SetTextStyleId(0, i,0,st[Extensions.curFont]);
                    tb.SetMargin(0, i, CellMargins.Left, 1);
                    //获取i行j列数据
                    //tb.Cells[0, i].TextHeight = 2.5;
                    //tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                    //tb.Cells[0, i].Borders.Horizontal.Margin = 1;
                    //tb.Cells[0, i].TextStyleId = st[Extensions.curFont];
                }

                for (int i = 0; i < theTable.Rows.Count; i++)
                {
                    for (int j = 0; j < theTable.Columns.Count; j++)
                    {
                        
                        //tb.Cells[i + 1, j].TextString = theTable.Rows[i][j].ToString();
                        //tb.Cells[i + 1, j].TextHeight = 2.5;
                        //tb.Cells[i + 1, j].Alignment = CellAlignment.MiddleCenter;
                        //tb.Cells[i + 1, j].Borders.Horizontal.Margin = 1;
                        //tb.Cells[i + 1, j].TextStyleId = st[Extensions.curFont];
                        tb.SetTextString(i + 1, j, theTable.Rows[i][j].ToString());
                        tb.SetTextHeight(i + 1, j, 2.5);
                        tb.SetAlignment(i + 1, j, CellAlignment.MiddleCenter);
                        tb.SetTextStyleId(i + 1, j, 0, st[Extensions.curFont]);
                        tb.SetMargin(i + 1, j, CellMargins.Left, 1);
                    }

                }
                tb.SetRowHeight(rowH);
                tb.SetColumnWidth(columnW);
                tb.GenerateLayout();
                tb.Layer = "H标注";
                modelSpace.AppendEntity(tb);
                tr.AddNewlyCreatedDBObject(tb, true);
                ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));
       
                DBText title = new DBText()
                {
                    TextString = theTable.TableName,
                    Height = 3.0,
                    TextStyleId = st[Extensions.curFont],
                    Position = midtop.C3D(0, 3),
                    HorizontalMode = TextHorizontalMode.TextMid,
                    VerticalMode = TextVerticalMode.TextBase,
                    AlignmentPoint = midtop.C3D(0, 3),
                    WidthFactor = ((TextStyleTableRecord)tr.GetObject(st[Extensions.curFont], OpenMode.ForRead)).XScale,
                };
                modelSpace.AppendEntity(title);
                tr.AddNewlyCreatedDBObject(title, true);
                ext = ext.Add(new Extents2d(title.Bounds.Value.MinPoint.C2D(), title.Bounds.Value.MaxPoint.C2D()));
                PLPloter.AddPloy4(db,midtop, w * 0.5, w * 0.5, h, ref ext,"H粗线");

                tr.Commit();
            }

         
        }
    }
}
